package com.example.proxymediaplayer;

import java.io.RandomAccessFile;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

import com.db.SdcardLocationDbHelper;
import com.proxy.ServerProxy;
import com.proxy.ServerProxy.UrlListener;

public class MainActivity extends Activity {

	private MediaController mediaController;
	private VideoView mVideoView;
	protected long startTimeMills;

	private ServerProxy proxy;

	protected String TAG = MainActivity.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		setTitle("玩转 Android MediaPlayer之视频预加载(优化)---hellogv");

		// 初始化VideoView
		mediaController = new MediaController(this);
		mVideoView = (VideoView) findViewById(R.id.surface_view);
		mVideoView.setMediaController(mediaController);
		mVideoView.setOnPreparedListener(mOnPreparedListener);

//		proxy = new ServerProxy();
//		proxy.startProxy("http://www.wecreate.top/test.mp4");

		delayToStartPlay.sendEmptyMessageDelayed(0, 1000);
		
		SdcardLocationDbHelper helper = new SdcardLocationDbHelper(this);
		SQLiteDatabase db = helper.getWritableDatabase();
		
	}

	private OnPreparedListener mOnPreparedListener = new OnPreparedListener() {

		@Override
		public void onPrepared(MediaPlayer mp) {
			mVideoView.start();
			long duration = System.currentTimeMillis() - startTimeMills;
			Log.e(TAG, "缓冲时间:" + duration);
		}
	};

	private Handler delayToStartPlay = new Handler() {
		public void handleMessage(Message msg) {
			startTimeMills = System.currentTimeMillis();
//			proxy.getLocalURL(new UrlListener() {
//
//				@Override
//				public void onResult(String proxyUrl) {
//					Log.e(TAG, "代理url:" + proxyUrl);
//					mVideoView.setVideoPath(proxyUrl);
//				}
//			});

			mVideoView.setVideoPath("http://www.wecreate.top/test.mp4");

		}
	};

}
